#include <bits/stdc++.h>
using namespace std;

struct TreeNode
{
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr)
    {
    }
};
class Solution
{
  public:
    TreeNode *prev = nullptr;
    bool isValidBST(TreeNode *root)
    {
        // write code here
        if (root == nullptr)
        {
            return true;
        }
        bool left = isValidBST(root->left);

        if (prev != nullptr && root->val < prev->val)
            return false;
        prev = root;
        bool right = isValidBST(root->right);

        isValidBST(root->right);
        return left && right;
    }
};